 package top.lyc829.zzl_android.ui.profile.user_dtc.dynamic;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.DynamicBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class UserDynamicViewModel extends ViewModel {

    private MutableLiveData<List<DynamicBean>> userDynamicLiveData;
    //alt+inset;  get
    public MutableLiveData<List<DynamicBean>> getUserDynamicLiveData() {
        if (userDynamicLiveData == null)
        {
            //实例化
            userDynamicLiveData=new MutableLiveData<>();
        }
        return userDynamicLiveData;
    }


    public void getUserDynamicList(int user_id, int page)
    {

        Network.getUserDynamicList(user_id, page, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr=res.body().string();
                ResultBean<List<DynamicBean>> resultBean=JsonUtilNew.fromJsonArray(resultStr,DynamicBean.class);
                if (resultBean.getCode().equals(200))
                {
                    getUserDynamicLiveData().postValue(resultBean.getData());
                }
                else{
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                getUserDynamicLiveData().postValue(null);
                Utils.Sout(e.getMessage());
            }
        });

    }
}
